﻿using System;
using System.Linq.Expressions;
using Xtensive.Orm;

namespace PartialIndex.Model
{
    /*
     * To define partial index you should provide a method of Expression<Func<T, bool>> type that will be used to 
     * filter entities for inclusion in index.
     * The method can be declared in a separate static class.
     * However, you might want to define filters right inside entity class, see Person.cs for details.
     * 
     * More on partial indexes: http://en.wikipedia.org/wiki/Partial_index
     */
    [HierarchyRoot]
    [Index("FireDate", FilterType = typeof(Filters), Filter = "FiredEmployeesFilter")]
    public class Employee : Entity
    {
        [Field, Key]
        public int Id { get; private set; }

        [Field]
        public DateTime? FireDate { get; set; }

        [Field]
        public string FirstName { get; set; }

        [Field]
        public string LastName { get; set; }

        public Employee(Session session)
            : base(session)
        {}
    }

    public static class Filters
    {
        private static Expression<Func<Employee, bool>> FiredEmployeesFilter()
        {
            return employee => employee.FireDate != null;
        }
    }
}
